import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { isLogin, setToken } from "@/utils/common";
import userApi from "@/api/user";
import store from "@/store/store";

const routes = [
  {
    path: "/test",
    name: "test",
    component: () => import("@/components/test.vue"),
  },
  {
    path: "/",
    name: "main",
    component: () => import("@/views/main/MainPage.vue"),
    children: [
      {
        path: "/",
        name: "index",
        component: () => import("@/views/main/first/First.vue"),
      },
      {
        path: "/rank",
        name: "rank",
        component: () => import("@/views/main/rank/Rank.vue"),
      },
      {
        path: "/channel/:id",
        name: "channel",
        component: () => import("@/views/main/channel/Channel.vue"),
      },
    ],
  },
  {
    path: "/home/:id",
    name: "person",
    component: () => import("@/views/home/Home.vue"),
    children: [
      {
        path: "/home/:id",
        name: "home",
        component: () => import("@/views/home/videos/MyVideos.vue"),
      },
      {
        path: "/home/userinfo/:id",
        name: "personal",
        component: () => import("@/views/home/personal/Personal.vue"),
      },
      {
        path: "/home/safety/:id",
        name: "safety",
        component: () => import("@/views/home/safe/Safe.vue"),
      },
      {
        path: "/home/collection/:id",
        name: "collection",
        component: () => import("@/views/home/collection/Collection.vue"),
      },
    ],
  },
  {
    path: "/video/:id",
    name: "video",
    component: () => import("@/views/video/Video.vue"),
  },
  {
    path: "/search/:content",
    name: "search",
    component: () => import("@/views/search/Search.vue"),
  },
  {
    path: "/platform",
    name: "platform",
    component: () => import("@/views/create-center/Center.vue"),
    children: [
      {
        path: "/platform/upload",
        name: "upload",
        component: () => import("@/views/create-center/upload/Upload.vue"),
      },
      {
        path: "/platform/manage",
        name: "manage",
        component: () => import("@/views/create-center/manage/Manage.vue"),
      },
      {
        path: "/platform/data",
        name: "data",
        component: () => import("@/views/create-center/data/Data.vue"),
      },
      {
        path: "/platform/comment",
        name: "commentInteract",
        component: () =>
          import("@/views/create-center/interact/CommentInteract.vue"),
      },
      {
        path: "/platform/danmaku",
        name: "DanmakuInteract",
        component: () =>
          import("@/views/create-center/interact/DanmakuInteract.vue"),
      },
    ],
    meta: {
      requireAuth: true, // 在需要登录的路由的meta中添加响应的权限标识
    },
  },
];

const routerHistory = createWebHistory();
const routers = createRouter({
  history: routerHistory,
  routes: routes,
});
routers.beforeEach(async (to, from, next) => {
  try {
    // 检查访问的路径是否需要登录
    let flag: boolean = to.matched.some((record) => record.meta.requireAuth);
    if (flag) {
      if (isLogin()) {
        next();
      } else {
        // @ts-ignore
        await userApi.autoLogin().then((res) => {
          if (res.data.code === 0) {
            store().setUserInfo(res.data.data);
            setToken(res.data.data.token);
            next();
          } else {
            next({ path: from.path });
            store().setLoginBox(true);
          }
        });
      }
    } else {
      next();
    }
  } catch (ignore) {}
});
export default routers;
